package org.zcode.generator;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.zcode.generator.core.config.BeanTypeConfig;
import org.zcode.generator.core.config.XmlConfig;
import org.zcode.generator.core.generator.ClazzCreator;
import org.zcode.generator.core.generator.clazz.AtomServiceCreator;
import org.zcode.generator.core.generator.clazz.DAOTestClazzCreator;
import org.zcode.generator.core.generator.clazz.DatasourceServiceCreator;
import org.zcode.generator.core.generator.clazz.DatasourceTestClazzCreator;
import org.zcode.generator.core.util.GeneratorUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 生成service类
 *
 * @author: zhoulk
 * @date: 2019年10月30日 20:03:45
 */
public class MybatisServicePlugin extends PluginAdapter {
    private XmlConfig xmlConfig;
    private BeanTypeConfig beanTypeConfig;
    public MybatisServicePlugin() {
        super();
    }

    @Override
    public boolean validate(List<String> warnings) {
        xmlConfig = XmlConfig.init(properties);
        xmlConfig.baseBeanPack = context.getJavaModelGeneratorConfiguration().getTargetPackage();
        xmlConfig.conditionBeanPack = context.getJavaExampleGeneratorConfiguration().getTargetPackage();
        return true;
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        String table = introspectedTable.getBaseRecordType();
        String tableName = table.substring(table.lastIndexOf(".") + 1);
        beanTypeConfig = BeanTypeConfig.init(introspectedTable, xmlConfig);

        List<GeneratedJavaFile> files = new ArrayList<>();
        ClazzCreator clazzCreator;
        if (xmlConfig.enableAtom) {
            files.addAll(createBaen(introspectedTable, tableName));
            clazzCreator = new AtomServiceCreator(xmlConfig, beanTypeConfig, introspectedTable);
            files.addAll(clazzCreator.create());
        }
        if (xmlConfig.enableDatasource) {
            clazzCreator = new DatasourceServiceCreator(xmlConfig, beanTypeConfig, introspectedTable);
            files.addAll(clazzCreator.create());
        }
        if (xmlConfig.enableTest) {
            clazzCreator = new DatasourceTestClazzCreator(xmlConfig, beanTypeConfig, introspectedTable);
            files.addAll(clazzCreator.create());
            clazzCreator = new DAOTestClazzCreator(xmlConfig, beanTypeConfig, introspectedTable);
            files.addAll(clazzCreator.create());
        }
        return files;
    }


    /**
     * 创建原子服务接口bean
     * @param: introspectedTable
     * @param: tableName
     * @return:
     */
    private List<GeneratedJavaFile> createBaen(IntrospectedTable introspectedTable, String tableName) {
        String atomIPoPack_ = xmlConfig.atomIPoPack + "." + tableName.toLowerCase().substring(xmlConfig.substring);
        String atomOPoPack_ = xmlConfig.atomOPoPack + "." + tableName.toLowerCase().substring(xmlConfig.substring);
        String tableRemarks = GeneratorUtil.getTableRemarks(introspectedTable.getRemarks());
        GeneratedJavaFile updIpoFile = GeneratorUtil.createEntity(introspectedTable,
                beanTypeConfig.updIPoType, xmlConfig.atomPoTarget, atomIPoPack_,
                "Upd" + tableName.substring(xmlConfig.substring) + "IPo", tableRemarks + "更新接口入参", xmlConfig.author);
        GeneratedJavaFile addIpoFile = GeneratorUtil.createEntity(introspectedTable,
                beanTypeConfig.addIPoType, xmlConfig.atomPoTarget, atomIPoPack_,
                "Add" + tableName.substring(xmlConfig.substring) + "IPo", tableRemarks + "新增接口入参", xmlConfig.author);
        GeneratedJavaFile qrysIPoFile = GeneratorUtil.createEntity(introspectedTable,
                beanTypeConfig.qrysIPoType, xmlConfig.atomPoTarget, atomIPoPack_,
                "Qry" + tableName.substring(xmlConfig.substring) + "sIPo", tableRemarks + "列表查询接口入参", xmlConfig.author);
        GeneratedJavaFile qryOPoFile = GeneratorUtil.createEntity(introspectedTable,
                beanTypeConfig.qryOPoType, xmlConfig.atomPoTarget, atomOPoPack_,
                "Qry" + tableName.substring(xmlConfig.substring) + "OPo", tableRemarks + "根据ID查询接口出参", xmlConfig.author);
        GeneratedJavaFile qryOIPoFile = GeneratorUtil.createEntity(introspectedTable,
                beanTypeConfig.qrysOPoType, xmlConfig.atomPoTarget, atomOPoPack_,
                "Qry" + tableName.substring(xmlConfig.substring) + "sOPo", tableRemarks + "列表查询接口出参", xmlConfig.author);
        return new ArrayList<>(Arrays.asList(updIpoFile, addIpoFile, qrysIPoFile, qryOPoFile, qryOIPoFile));
    }
}
